//
//  MCGoodsManageSettingView.swift
//  StoreManage
//
//  Created by MC on 2017/12/20.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCGoodsManageSettingViewDelegate : NSObjectProtocol{
    func MCGoodsManageSettingView_editStoreProductInfo(count:Int,sale:Double,settle:Double,productIdList:NSArray,status:Int)
}

class MCGoodsManageSettingView: UIView {

    var productIdList : NSArray?
        
    
    weak var delegate : MCGoodsManageSettingViewDelegate?
    
    private let whiteHeight : CGFloat = 370
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        
        
        self.addSubview(whiteView)
        self.whiteView.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(self.snp.top).offset(0)
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize.init(width: MCScreenWidth - 40, height: self.whiteHeight))
        }

        
        whiteView.addSubview(deleteButton)
        deleteButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(whiteView).offset(25)
            make.top.equalTo(whiteView).offset(-30)
            make.size.equalTo(CGSize.init(width: 60, height: 60))
        }
        
        
        // 商品模块
        whiteView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(whiteView).offset(10)
            make.top.equalTo(whiteView).offset(-15)
            make.size.equalTo(CGSize.init(width: 90, height: 90))
        }
        whiteView.addSubview(totalLabel)
        totalLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.bottom.equalTo(iconImageView).offset(-2)
        }

        
        whiteView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.bottom.equalTo(totalLabel.snp.top).offset(-5)
        }
        
        whiteView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.right.equalTo(deleteButton.snp.left).offset(-8)
            make.top.equalTo(whiteView).offset(2)
            make.bottom.lessThanOrEqualTo(moneyLabel.snp.top).offset(-5)
        }
        
        
        whiteView.addSubview(countView)
        countView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(whiteView).offset(15)
            make.right.equalTo(whiteView).offset(-15)
            make.top.equalTo(iconImageView.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        whiteView.addSubview(saleView)
        saleView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(countView)
            make.top.equalTo(countView.snp.bottom).offset(0)
            make.height.equalTo(50)
        }

        whiteView.addSubview(settleView)
        settleView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(countView)
            make.top.equalTo(saleView.snp.bottom).offset(0)
            make.height.equalTo(50)
        }

        whiteView.addSubview(startUseView)
        startUseView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(countView)
            make.top.equalTo(settleView.snp.bottom).offset(0)
            make.height.equalTo(50)
        }

        
        
        whiteView.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(whiteView).offset(20)
            make.right.equalTo(whiteView).offset(-20)
            make.bottom.equalTo(whiteView).offset(-10)
            make.height.equalTo(32)
        }
        
        
        UIView.animate(withDuration: 0.1, animations: {
            self.whiteView.snp.remakeConstraints { (make) ->Void in
                make.bottom.equalTo(self.snp.top).offset(0)
                make.centerX.equalTo(self)
                make.size.equalTo(CGSize.init(width: MCScreenWidth - 40, height: self.whiteHeight))
            }
            self.layoutIfNeeded()
        }) { (animate) in
            UIView.animate(withDuration: 0.5, animations: {
                self.whiteView.snp.remakeConstraints { (make) ->Void in
                    make.bottom.equalTo(self.snp.top).offset(self.whiteHeight + 90)
                    make.centerX.equalTo(self)
                    make.size.equalTo(CGSize.init(width: MCScreenWidth - 40, height: self.whiteHeight))
                }
                self.layoutIfNeeded()
            })
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func deleteButtonClicked() {
        self.removeFromSuperview()        
    }
    
    @objc func reduceButtonClicked() {
        
        var goodsCount = Int(countView.textField.text!) ?? 0
        
        
        
        goodsCount -= 1
        if goodsCount <= 0 {
            goodsCount = 0
        }
        countView.textField.text = String(goodsCount)
    }
    
    @objc func addButtonClicked() {
        var goodsCount = Int(countView.textField.text!) ?? 0
        goodsCount += 1
        countView.textField.text = String(goodsCount)
    }
    
    @objc func startUseViewEvent() {
        startUseView.selectedButton.isSelected = !startUseView.selectedButton.isSelected
    }
    
    @objc func saveButtonClicked() {
        
        guard (countView.textField.text!.count > 0 && saleView.textField.text!.count >= 0 && settleView.textField.text!.count > 0) else {
            MCHUD_text(message: "请填写完整货品信息", view: self, delay: 1.5)
            return
        }
        
        
        
        self.endEditing(true)
        self.removeFromSuperview()
        let count = Int(countView.textField.text!)
        let sale = Double(saleView.textField.text!)
        let settle = Double(settleView.textField.text!)
        
        var status : Int
        if startUseView.selectedButton.isSelected {
            status = 1
        } else {
            status = 0
        }
        
        self.delegate?.MCGoodsManageSettingView_editStoreProductInfo(count: count!, sale: sale!, settle: settle!, productIdList: productIdList!,status: status)
    }

    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    public lazy var deleteButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_delete"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(deleteButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 5
        imageView.layer.borderColor = UIColor.white.cgColor
        imageView.layer.borderWidth = 2
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()
    
    
    public lazy var nameLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(13)
        label.textColor = UIColor.MCBlack
        label.numberOfLines = 0
        return label
    }()
    
    public lazy var moneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCBlack
        return label
    }()
    
    public lazy var totalLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCBlack
        return label
    }()
    
    lazy var countView: MCGoodsManageSetting_countView = {
        let view = MCGoodsManageSetting_countView()
        view.titleLabel.text = "库存(个)"
        view.textField.text = "1"
        view.textField.becomeFirstResponder()
        view.textField.keyboardType = UIKeyboardType.numberPad
        view.reduceButton.addTarget(self, action: #selector(reduceButtonClicked), for: UIControlEvents.touchUpInside)
        view.addButton.addTarget(self, action: #selector(addButtonClicked), for: UIControlEvents.touchUpInside)
        return view
    }()
    
    lazy var saleView: MCGoodsManageSetting_priceView = {
        let view = MCGoodsManageSetting_priceView()
        view.titleLabel.text = "店铺售价(¥)"
        view.textField.text = "0.01"
        view.textField.keyboardType = UIKeyboardType.decimalPad
        view.textField.delegate = self
        return view
    }()
    
    lazy var settleView: MCGoodsManageSetting_priceView = {
        let view = MCGoodsManageSetting_priceView()
        view.titleLabel.text = "结算价(¥)"
        view.textField.keyboardType = UIKeyboardType.decimalPad
        view.textField.delegate = self
        return view
    }()
    
    lazy var startUseView: MCAfterSaleDetailFooter_selectedView = {
        let view = MCAfterSaleDetailFooter_selectedView()
        view.nameLabel.text = "是否启用"
        view.selectedButton.isSelected = true
        view.selectedButton.addTarget(self, action: #selector(startUseViewEvent), for: UIControlEvents.touchUpInside)
        return view
    }()
    
    public lazy var saveButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 16
        button.layer.masksToBounds = true
        button.titleLabel?.font = MCFont(14)
        button.setTitle("保存", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(saveButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

}



extension MCGoodsManageSettingView : UITextFieldDelegate {

    // UITextField 只能输入数字和小数点，只能有两位小数
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let b = MCString_keepTwoDecimalPlaces(textField, range, string)
        return b
    }
}































